// pages/integral-index/integral-index.js
import { getSalesIntegralProducts, getIntegral, insertIntegralOrder, mainInList } from '../../api/apis.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: [],
    productList: [],
    activeProduct: {},
    productModal: false,
    pageIndex: 0,
    order: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initData()
  },
  _initData() {
    this._getSalesIntegralProducts()
    this._getIntegral()
    this._getBanner()
  },
  _getBanner() {
    mainInList({
      data: {
        index: 1,
        size: 5,
        categorys: 178,
        menuID: 5
      },
      success: res => {
        if (res.state) {
          res.data.list.map((item) => {
            item.imgs = item.images.length > 0 ? item.images[0].URL : ""
            item.time = item.publishDate
            return item
          })
          this.setData({
            'banners': res.data.list
          })
        }
      }
    })
  },
  _getSalesIntegralProducts() {
    this.data.pageIndex ++
    getSalesIntegralProducts({
      data: {
        "sortType": '',
        "category": '',
        "size": this.data.pageIndex,
        "length": 20
      },
      success: res => {
        if (res.state) {
          if (res.data.length>0) {
            this.setData({
              productList: this.data.productList.concat(res.data)
            })
          } else {

          }
        }
      }
    })
  },
  _getIntegral() {
    getIntegral({
      data: {
        openid: app.globalData.openid
      },
      success: res => {
        if (res.state) {
          this.setData({
            integral: res.data
          })
        }
      }
    })
  },
  productClick(e) {
    wx.navigateTo({
      url: '../integral-detail/integral-detail?did=' + e.currentTarget.dataset.product.id,
    })
    // this.setData({
    //   productModal: true,
    //   activeProduct: e.currentTarget.dataset.product
    // })
  },
  modalClick() {
    this.setData({
      productModal: false
    })
  },
  formOrder() {
    var obj = {}
    obj.name = this.data.activeProduct.name
    obj.sku = this.data.activeProduct.sku[0].title
    obj.img = this.data.activeProduct.img
    obj.price = this.data.activeProduct.sku[0].price
    obj.count = 1
    obj.productId = this.data.activeProduct.id
    var pro = []
    pro.push(obj)
    this.data.order.express_fee = '暂未需要';
    this.data.order.remark = '暂无备注';
    this.data.order.order_state = "待发货";
    this.data.order.buy_openid = app.globalData.openid;
    // order.shareOpenid = shareopenid;
    this.data.order.products = pro;
    this.data.order.order_total_integral = parseFloat(obj.price) * parseFloat(obj.count);
    this.data.order.cid = 'admins';
  },
  exchange() {
    wx.showModal({
      title: '积分兑换',
      content: `您将花费${this.data.activeProduct.sku[0].price}积分兑换${this.data.activeProduct.name}`,
      success: re => {
        if (re.confirm) {
          this.formOrder()
          insertIntegralOrder({
            data: {
              order:JSON.stringify(this.data.order),
              payType: "1"
            },
            success: res => {
              if (res.state) {
                wx.showToast({
                  title: '兑换成功！',
                  icon: 'none'
                })
                this.setData({
                  productModal: false,
                  activeProduct: {}
                })
                this._initData()
              } else {
                wx.showToast({
                  title: res.message,
                  icon: 'none'
                })
              }
            }
          })
        } else if (re.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onReachBottom() {
    this._getSalesIntegralProducts()
  }
})